#!usr/bin/env python
# -*- coding:utf-8 -*-

# @Author:   tian
# @project:  db.py

from redis import StrictRedis
from config import *
import random

class RedisServer:
    def __init__(self):
        self.db = StrictRedis(host=HOST,port=PORT,password=PASSWORD,db=0,decode_responses=True)
        self.name = 'ip'
        self._name = 'count'

    def getall(self):
        '''
        获取所有元素
        :return:
        '''
        # return self.db.zrange(self.name,0,-1,withscores=True,score_cast_func=int)
        return self.db.zrange(self.name, 0, -1, withscores=False)

    def add(self, value, score):
        return self.db.zadd(self.name, {value: score})

    def count(self):
        return self.db.zcard(self.name)

    def random(self):
        return random.choice(self.getall())

    def score(self, value):
        '''
        返回分数值
        :param value:
        :return:
        '''
        return self.db.zscore(self.name, value)

    def delete(self, value):
        return self.db.zrem(self.name, value)

    def incrby(self, value, count):
        return self.db.zincrby(self.name, count, value)

    def lpush(self, value):
        return self.db.lpush(self._name, value)

    def llen(self):
        return self.db.llen(self._name)


if __name__ == '__main__':
    rs = RedisServer()